package com.hangxin.basic.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hangxin.basic.dao.UserLoginDao;
import com.hangxin.basic.mapping.LoginInfoRowMapper;
import com.hangxin.basic.model.LoginInfo;
import com.hangxin.basic.service.UserLoginService;
import com.hangxin.commons.service.impl.BasicServiceImpl;
import com.hangxin.commons.util.PageModel;
import com.hangxin.commons.util.PageValidate;

/**
 * 
 * 类名称：UserLoginServiceImpl.java
 * <P/>
 * 类描述： 用户操作的service接口实现类
 * <P/>
 * 创建时间：2013年3月10日
 * <P/>
 * 创建人： 李志远
 * <P/>
 * 联系方式： 1726021389@qq.com
 * <P/>
 * 修改人：无
 * <P/>
 * 修改时间：无
 * <P/>
 * 修改备注：无
 * <P/>
 * 版本：v1.0
 * 
 */
@Service
public class UserLoginServiceImpl extends BasicServiceImpl implements UserLoginService
{

	static final Logger logger = LoggerFactory.getLogger(UserLoginServiceImpl.class);

	@Resource
	private UserLoginDao userLoginDaoImpl;

	private final int pagesize = 15;



	/**
	 * 
	 * 功能描述：判断用户名和密码是否存在
	 * <P/>
	 * 创建时间：2013年3月10日
	 * <P/>
	 * 创建人： 李志远
	 * <P/>
	 * 联系方式： 1726021389@qq.com
	 * 
	 * @param username用户名
	 * @param password密码
	 * @return
	 */
	@Override
	public int ifExist(String username, String password)
	{

		return userLoginDaoImpl.ifExist(username, password);
	}



	/**
	 * 
	 * 功能描述：根据用户名和密码查询出用户信息
	 * <P/>
	 * 创建时间：2013年3月10日
	 * <P/>
	 * 创建人： 李志远
	 * <P/>
	 * 联系方式： 1726021389@qq.com
	 * 
	 * @param username
	 * @param password
	 * @return
	 */
	@Override
	public LoginInfo findUserLogin(String username, String password)
	{
		return userLoginDaoImpl.findUserLogin(username, password);
	}

	@Override
	public LoginInfo findLoginInfoById(String loginid) {
		return userLoginDaoImpl.findLoginInfoById(loginid);
	}

	@Override
	public List<LoginInfo> getListByDepartid(String departid)
	{
		String sql = "select logininfo.* from logininfo where ";
		Object[] params = new Object[] { departid };
		return userLoginDaoImpl.findAll(sql, params, new LoginInfoRowMapper());
	}



	@Override
	public PageModel<Map<String, Object>> selectList(String _pageNo, String _pages, String condition)
	{
		int pageNo = PageValidate.pageNoValidate(_pageNo);
		int pages = PageValidate.pageNoValidate(_pages);
		PageModel<Map<String, Object>> pm = new PageModel<Map<String, Object>>(pageNo, pages, pagesize);
		StringBuffer sql = new StringBuffer();
		sql.append("select top " + pagesize + " userlogin.*,department.name departname from userlogin,department where userlogin.departid=department.departid and userid not in (select top " + (pm.getPageNo() - 1) * pagesize + " userid from userlogin,department where userlogin.departid=department.departid " + condition + ") " + condition + "");
		StringBuffer sqlcount = new StringBuffer();
		sqlcount.append("select count(*) from userlogin,department where userlogin.departid=department.departid " + condition + "");
		if (logger.isDebugEnabled())
			logger.debug(sql.toString());
		List<Object> params = new ArrayList<Object>();
		List<Object> paramcounts = new ArrayList<Object>();

		pm = userLoginDaoImpl.listByObject(sqlcount.toString(), sql.toString(), pm, paramcounts.toArray(), params.toArray());
		return pm;
	}



	@Override
	@Transactional
	public void save(LoginInfo userLogin, String roleid)
	{
		// String sql="insert into userlogin(username,realname,password,xb,zw,bgdh,jtdh,sjhm,email,jtdz,yzbm,sfgly,cjrq,remark,departid) values(?,?,?,?,?,?,?,?,?,?,?,?,getdate(),?,?)";
		// Object[] params= new Object[]{userLogin.getName(),userLogin.getName(),MD5Util.encode("123456"),userLogin.getName(),userLogin.getZw(),
		// userLogin.getName(),userLogin.getName(),userLogin.getName(),userLogin.getName(),userLogin.getName(),userLogin.getYzbm(),userLogin.getSfgly(),userLogin.getRemark(),userLogin.getDepartid()};
		// if(logger.isDebugEnabled())
		// logger.debug(sql.toString());
		// userLoginDaoImpl.save(sql, params);
		// String sql1="insert into userrole(username,roleid) values (?,?)";
		// Object[] params1=new Object[]{userLogin.getName(),roleid};
		// userLoginDaoImpl.save(sql1, params1);
	}



	@Override
	public LoginInfo findUserLoginByUserid(int userid)
	{
		String sql = "select userlogin.*,department.name departname from userlogin,department where userlogin.departid=department.departid and userlogin.userid=?";
		Object[] params = new Object[] { userid };
		LoginInfo userLogin = userLoginDaoImpl.find(sql, params, new LoginInfoRowMapper());
		return userLogin;
	}



	@Override
	@Transactional
	public void update(LoginInfo userLogin, String roleid)
	{
		// String sql="update userlogin set username=?,realname=?,xb=?,zw=?,bgdh=?,jtdh=?,sjhm=?,email=?,jtdz=?,yzbm=?,sfgly=?,remark=?,departid=? where userid=?";
		// Object[] params= new Object[]{userLogin.getUsername(),userLogin.getRealname(),userLogin.getXb(),userLogin.getZw(),
		// userLogin.getBgdh(),userLogin.getJtdh(),userLogin.getSjhm(),userLogin.getEmail(),userLogin.getJtdz(),userLogin.getYzbm(),userLogin.getSfgly(),userLogin.getRemark(),userLogin.getDepartid(),userLogin.getUserid()};
		// if(logger.isDebugEnabled())
		// logger.debug(sql.toString());
		// userLoginDaoImpl.edit(sql, params);
		// String sql1="update userrole set roleid=? where username=?";
		// Object[] params1=new Object[]{roleid,userLogin.getUsername()};
		// userLoginDaoImpl.edit(sql1, params1);
	}



	@Override
	public int delete(int userid)
	{
		String sql = "delete from userlogin where userid=?";
		Object[] params = new Object[] { userid };
		if (logger.isDebugEnabled())
			logger.debug(sql.toString());
		return userLoginDaoImpl.delete(sql, params);
	}



	@Override
	public int findUserLoginByUsername(String username)
	{
		String sql = "select count(*) from userlogin,department where userlogin.departid=department.departid and userlogin.username=?";
		Object[] params = new Object[] { username };
		if (logger.isDebugEnabled())
			logger.debug(sql.toString() + "====" + username);
		int count = 0;
		if (!username.equals(""))
			count = userLoginDaoImpl.findByCount(sql, params);
		return count;
	}


	/**
	 * 根据lgid修改登陆密码
	 * @param userid
	 * @return
	 */
	@Override
	public void changepassword(String newpassword, String logid) {
		// TODO Auto-generated method stub
		userLoginDaoImpl.changepassword(newpassword, logid);
	}

}
